package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class DtsAdminServiceImpl implements DtsAdminService {
    @Autowired
    DtsAdminMapper adminMapper;
    @Override
    public DtsAdmin findByUsername(String username) {
        LambdaQueryWrapper<DtsAdmin> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DtsAdmin::getUsername, username);
        DtsAdmin admin = adminMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public IPage<DtsAdmin> queryPage(Integer page, Integer limit, String sort, String order, String username) {

        IPage<DtsAdmin> ipage = new Page(page, limit);
        QueryWrapper<DtsAdmin> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(username)) {
            qw.like("username", username);
        }
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            } else {
                qw.orderByAsc(sort);
            }
        }

        adminMapper.selectPage(ipage , qw );
        return ipage;
    }

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        String password = admin.getPassword();
        DtsAdmin admin2 = adminMapper.selectById(admin.getId());
        String password2 = admin2.getPassword();
        if(!password.equalsIgnoreCase(password2)){
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String encode = passwordEncoder.encode(admin.getPassword());
            admin.setPassword(encode);
        }
        admin.setUpdateTime(new Date());
        adminMapper.updateById(admin);
        return admin;
    }

    @Override
    public DtsAdmin add(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(encode);
        adminMapper.insert(admin);
        return admin;
    }

    @Override
    public void deleteById(Integer anotherAdminId) {
        adminMapper.deleteById(anotherAdminId);
    }

    @Override
    public List<DtsAdmin> find() {
        QueryWrapper<DtsAdmin> qw = new QueryWrapper<>();
        List<DtsAdmin> admins = adminMapper.selectList(qw);
        return  admins;
    }
}
